package com.pmcc.core.service;

import com.pmcc.core.entity.Role;
import com.pmcc.core.repository.RoleRepository;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Tomas on 2018/4/8.
 */
@Service
@Transactional
public class RoleService {
    @Autowired
    RoleRepository roleRepository;

    public ResultBean queryRoleByPage(AntdPage antdPage,Role model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page=roleRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<Role> getSpecification(Role model) {
        return new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(StringUtil.isNotEmpty(model.getName())&&model.getName()!=null){
                    list.add(builder.like(root.get("name").as(String.class),  model.getName().trim()+"%"));
                }
                list.add(builder.equal(root.get("delFlag").as(Integer.class),0));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    public ResultBean addRole(String remark,String name,String code,String str,String str1){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Role role = new Role();
            if(StringUtil.isNotEmpty(code)){
                List<Role> list=roleRepository.findByCode(code);
                if(list.size()>0){
                    resultBean.setResultCode(ResultBean.FAIL);
                    resultBean.setResultDesc("编码重复！");
                    return resultBean;
                }
            }
            role.setName(name);
            role.setRemark(remark);
            role.setCode(code);
            role.setMenuKey(str);
            role.setMenuList(str1);
            role.setFettle(0);
            role.setDelFlag(0);
            role.setCreateTime(new Date());
            role.setCreator(OnlineUser.getName());
            roleRepository.saveAndFlush(role);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean editRole(Map map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Role c = roleRepository.findOne(map.get("id").toString());
            BeanUtils.setBeanData(c,map);
            roleRepository.saveAndFlush(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    public ResultBean deleteRole(String id) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            roleRepository.delete(id);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean findAll(){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try{
            List<Role> role=roleRepository.findByDelFlagAndFettle(0,0);
            resultBean.setRows(role);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
